package tw.arduino.view;
import org.apache.log4j.Logger;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;

import tw.arduino.module.PublicObj;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.StyledText;
/**
 * @ArduinoTW Command Center
 * @author XLINX x
 * @since 2010.08.08
 * @see http://arduino.tw
 */


public class PCSettingComposite extends Composite {
	static Logger log;
	private List list;
	private Combo baudCombo;
	private Combo combo_1;
	private Combo databitCombo;
	private Combo parityCombo;
	private Combo stopbitCombo;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */

	public boolean setAllComSetting(){
		try{
		String[] setting={list.getSelection()[0],baudCombo.getText(),parityCombo.getText(),databitCombo.getText(),stopbitCombo.getText()};
		PublicObj.setComSetting(setting);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	public PCSettingComposite(Composite parent, int style) {
		super(parent, style);
		log = Logger.getLogger(this.getClass().getName());
		setLayout(new GridLayout());
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Group grpComPortSetup = new Group(composite, SWT.NONE);
		grpComPortSetup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		grpComPortSetup.setText("Select Com Port");
		grpComPortSetup.setLayout(new GridLayout(1, false));
		
		list = new List(grpComPortSetup, SWT.BORDER);
		list.setItems(PublicObj.PortListArr);
		list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Group grpComPortDetial = new Group(composite, SWT.NONE);
		grpComPortDetial.setText("Com Port Detial");
		grpComPortDetial.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		grpComPortDetial.setLayout(new GridLayout(2, false));
		
		Label lblBaud = new Label(grpComPortDetial, SWT.NONE);
		lblBaud.setBounds(0, 0, 59, 14);
		lblBaud.setText("Baud");
		
		baudCombo = new Combo(grpComPortDetial, SWT.NONE);
		baudCombo.setItems(new String[] {"1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200"});
		baudCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		baudCombo.setBounds(0, 0, 26, 22);
		baudCombo.select(3);
		
		Label lblFlowcontrol = new Label(grpComPortDetial, SWT.NONE);
		lblFlowcontrol.setText("FlowControl");
		lblFlowcontrol.setBounds(0, 0, 59, 14);
		
		combo_1 = new Combo(grpComPortDetial, SWT.NONE);
		combo_1.setItems(new String[] {"NONE", "HARDWARE", "Xon/ Xoff"});
		combo_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		combo_1.setBounds(0, 0, 26, 22);
		combo_1.select(0);
		
		Label lblDatabits = new Label(grpComPortDetial, SWT.NONE);
		lblDatabits.setText("DataBits");
		lblDatabits.setBounds(0, 0, 59, 14);
		
		databitCombo = new Combo(grpComPortDetial, SWT.NONE);
		databitCombo.setItems(new String[] {"4", "5", "6", "7", "8"});
		databitCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		databitCombo.setBounds(0, 0, 26, 22);
		databitCombo.select(4);
		
		Label lblParity = new Label(grpComPortDetial, SWT.NONE);
		lblParity.setText("Parity");
		lblParity.setBounds(0, 0, 59, 14);
		
		parityCombo = new Combo(grpComPortDetial, SWT.NONE);
		parityCombo.setItems(new String[] {"NONE", "ODD", "EVEN", "MARK", "SPACE"});
		parityCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		parityCombo.setBounds(0, 0, 26, 22);
		parityCombo.select(0);
		
		Label lblStopbit = new Label(grpComPortDetial, SWT.NONE);
		lblStopbit.setText("StopBits");
		lblStopbit.setBounds(0, 0, 59, 14);
		
		stopbitCombo = new Combo(grpComPortDetial, SWT.NONE);
		stopbitCombo.setItems(new String[] {"1", "1.5", "2"});
		stopbitCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		stopbitCombo.setBounds(0, 0, 26, 22);
		stopbitCombo.select(0);
		
		Button btnSetToDefault = new Button(grpComPortDetial, SWT.NONE);
		btnSetToDefault.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnSetToDefault.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		btnSetToDefault.setBounds(0, 0, 94, 30);
		btnSetToDefault.setText("Save");
		
		Button btnReadFromDefault = new Button(grpComPortDetial, SWT.NONE);
		btnReadFromDefault.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnReadFromDefault.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		btnReadFromDefault.setText("Read from Default");
		
		Button btnRefreshComList = new Button(grpComPortDetial, SWT.NONE);
		btnRefreshComList.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				PublicObj.refreshPortListArr();
				list.setItems(PublicObj.PortListArr);
			}
		});
		btnRefreshComList.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		btnRefreshComList.setText("Refresh Com List");
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
